<template>
	<view class="management">
		<view class="dheader">
			<view class="shop">
				<image :src="shop_info.category_image" mode="aspectFill" class="Boutique"></image>
				<view class="title">
					{{shop_info.shop_name}}
				</view>
				<view class="right" @click="toggle">
					<view class="size">
						切换
					</view>
					<image :src="$IMG_URL('/static/home/indicate.png')" mode="aspectFill" class="icon"></image>
				</view>
			</view>
			<view class="search">
				<u-search placeholder="搜索优惠券面额" :show-action="false" shape="square" v-model="query.search" @change.stop="changeSearch"></u-search>
			</view>
		</view>
		<view class="tab">
			<view v-for="(item,index) in tabList" :key="index" class="tabIndex" @click="changeIndex(index)">
				<view :class="activeIndex == index ?'tabsize1':'tabsize2'">
					{{item}}
				</view>
				<view class="active">
					<view class="empty" v-if="activeIndex == index">

					</view>
				</view>
			</view>
		</view>
		<view class="managementFoot" @click="$ROUTELINK('add')">
			<view class="btn3">
				添加优惠券
			</view>
		</view>
		<scroll-view scroll-y="true" class="scroll-Y" @scrolltolower="scrolltolower">
			<view class="comlum_Y" v-if="couponList.length">
				<view v-for="(item,index) in couponList" :key="index" class="item">
					<couponVue :query="item">
						<view class="btn" :style="{opacity:!activeIndex?'1':'0.4'}" @click="routeGivePresent(item)">
							赠送
						</view>
						<view class="btn" :style="{opacity:!activeIndex?'1':'0.4'}" @click="openWeChatCode(item)">
							领取码
						</view>
						<view class="btn" @click.stop="$ROUTELINK('record?coupon_id='+ item.coupon_id)">
							记录
						</view>
						<view class="btn" @click.stop="$ROUTELINK('deatil?coupon_id=' + item.coupon_id)">
							查看详情
						</view>
					</couponVue>
				</view>
				<u-loadmore :status="status" />
			</view>
			<empty v-else :content="!activeIndex?'可领取优惠券':'已领完优惠券'"></empty>
		</scroll-view>
		<FieldPopupVue :popupShow="popupShow" :FieldList="Field" @sure="sure" @changeActiveIndex="changeActiveIndex"
			:activeIndex="FieldIndex"></FieldPopupVue>
		<WechatCode :WechatCodeShow="WechatCodeShow" :code="wechatCode" @save="saveImg" @sure="weChatSure"
			:title="'优惠券领取码'" :content="'扫码领取优惠券'" :btn="'保存到相册'"></WechatCode>

	</view>
</template>

<script>
	import FieldPopupVue from '../../pages/user/components/Field-popup.vue'
	import couponVue from './components/coupon.vue';
	import WechatCode from '@/components/WechatCode.vue'
	import empty from './components/empty.vue'
	import {
		atShop,
		storeCouponList,
		storeCouponQrcode,
		storeMemberPoster
	} from '@/api/manager.js'
	export default {
		components: {
			FieldPopupVue,
			couponVue,
			WechatCode,
			empty
		},
		data() {
			return {
				shop_info: {},
				query: {
					form: 'draw',
					search: '',
					page: 1
				},
				WechatCodeShow: false,
				wechatCode: '',
				wechatCodeBig:'',
				// 切换门店
				popupShow: false,
				Field: [],
				FieldIndex: -1,
				shopName: '温州中庚漫游天地店',
				// 导航
				tabList: [
					'可领取',
					'已领完',
				],
				activeIndex: 0,
				couponList: [],
				status: 'loadmore', //当前状态
			}
		},
		onLoad() {
			
		},
		onShow() {
			this.getatShop()
		},
		methods: {
			changeSearch(){
				this.couponList = []
				this.query.page = 1
				this.getstoreCouponList()
			},
			scrolltolower(){
				this.query.page++
				this.getstoreCouponList()
			},
			getstoreCouponList() {
				storeCouponList(this.query).then(({
					data: res
				}) => {
					console.log('res', res);
					if (res.code == 1) {
						this.status = 'loading'
						if (this.query.page > res.data.all_page) {
							console.log('nomore');
							this.status = 'nomore'
							return false
						} else {
							console.log('more');
							this.status = 'more'
							this.couponList = this.couponList.concat(res.data.list)
						}

						console.log('couponList', this.couponList);
					}
				})
			},
			getatShop(e) {
				atShop(e).then(({
					data: res
				}) => {
					console.log('res', res);
					if (res.code == 1) {
						this.shop_info = res.data
						this.couponList = []
						this.query.page = 1
						this.getstoreCouponList()
					}
				})
			},
			// 切换门店
			toggle() {
				console.log(11);
				this.popupShow = true
			},
			changeActiveIndex(item, index) {
				this.FieldIndex = index
				this.getatShop({
					shop_id: item.shop_id
				})
			},
			sure() {
				this.$SAVERIMG(this.wechatCode)
				this.popupShow = false
			},
			changeIndex(index) {
				if (this.activeIndex != index) {
					if (!index) {
						this.query.form = 'draw'
					} else {
						this.query.form = 'undraw'
					}
					this.activeIndex = index
					this.couponList = []
					this.query.page = 1
					this.getstoreCouponList()
				}

			},
			openWeChatCode(item) {
				if (!this.activeIndex) {
					this.getstoreCouponQrcode(item.coupon_id)
					this.getstoreMemberPoster({coupon_id:item.coupon_id})
					this.WechatCodeShow = true
				}
			},
			routeGivePresent(item) {
				if (!this.activeIndex) {
					uni.navigateTo({
						url: 'givePresent?coupon_id=' + item.coupon_id
					})
				}
			},
			getstoreCouponQrcode(e){
				storeCouponQrcode({
					coupon_id:e
				}).then(({data:res})=>{
					console.log('res', res);
					if(res.code == 1){
						this.wechatCode = res.data.qrcode
					}
				})
			},
			getstoreMemberPoster(e){
				storeMemberPoster(e).then(({data:res})=>{
					console.log('res', res);
					if(res.code == 1){
						this.wechatCodeBig = res.data.poster
					}
				})
			},
			weChatSure() {
				
				this.WechatCodeShow = false
			},
			saveImg() {
				this.getstoreMemberPoster()
				this.$SAVERIMG(this.wechatCodeBig)
				this.WechatCodeShow = false
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #e9e9e9;
	}

	.management {
		display: flex;
		flex-direction: column;
		align-items: center;

		.dheader {
			width: 750rpx;
			height: 184rpx;
			background: #ffffff;
			padding: 26rpx 34rpx 0;

			.shop {
				width: 100%;
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 22rpx;

				.Boutique {
					width: 114rpx;
					height: 36rpx;
					margin-right: 12rpx;
				}

				.title {
					flex: 1;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC-Bold;
					font-weight: 700;
					text-align: left;
					color: #333333;
				}

				.right {
					display: flex;
					flex-direction: row;
					align-items: center;

					.size {
						font-size: 28rpx;
						font-family: PingFang SC, PingFang SC-Regular;
						font-weight: 400;
						color: #666666;
						margin-right: 8rpx;
					}

					.icon {
						width: 10rpx;
						height: 16rpx;
					}
				}
			}

			.search {
				width: 100%;
			}


		}

		.tab {
			width: 750rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-around;
			background: #ffffff;

			.tabIndex {
				display: flex;
				flex-direction: column;
				align-items: center;

				.tabsize1 {
					height: 44rpx;
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC-Heavy;
					font-weight: 800;
					text-align: center;
					color: #73F0EA;
					letter-spacing: 0.7rpx;

				}

				.tabsize2 {
					height: 44rpx;
					font-size: 32rpx;
					font-family: PingFang SC, PingFang SC-Regular;
					font-weight: 400;
					text-align: center;
					color: #666666;
					letter-spacing: 0.7rpx;

				}

				.active {
					margin-top: 12rpx;
					width: 44rpx;
					height: 8rpx;

					.empty {
						width: 100%;
						height: 100%;
						background: #73F0EA;
						border: 2rpx solid rgba(0, 0, 0, 0.00);
						border-radius: 2rpx;
					}
				}
			}
		}

		.scroll-Y {
			height: 72vh;
			width: 100%;

			.comlum_Y {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding-top: 24rpx;

				.btn {
					transform: rotateZ(360deg);
					width: 152rpx;
					height: 64rpx;
					border: 1rpx solid #73F0EA;
					border-radius: 16rpx;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC-Bold;
					font-weight: 700;
					text-align: center;
					color: #73F0EA;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.item {
					width: 702rpx;
					height: 256rpx;
					background: #ffffff;
					border-radius: 16rpx;
					margin-bottom: 20rpx;
				}
			}
		}

		.managementFoot {
			position: fixed;
			bottom: 0;
			width: 750rpx;
			height: 160rpx;
			background: #ffffff;
			box-shadow: 0rpx 8rpx 28rpx 0rpx rgba(166, 179, 194, 0.30);
			padding-top: 10rpx;
			display: flex;
			justify-content: center;

			.btn3 {
				width: 710rpx;
				height: 88rpx;
				background: #73F0EA;
				border-radius: 16rpx;
				font-size: 34rpx;
				font-family: PingFang SC, PingFang SC-Bold;
				font-weight: 700;
				color: #ffffff;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>